@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: PC微信聊天应用部署脚本 (Windows版本)
:: 使用方法: deploy.bat [dev|prod|install|build|help]

title PC微信聊天应用部署脚本

:: 颜色定义
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

:: 打印带颜色的消息
:print_message
echo %GREEN%[INFO]%NC% %~1
goto :eof

:print_warning
echo %YELLOW%[WARN]%NC% %~1
goto :eof

:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof

:print_step
echo %BLUE%[STEP]%NC% %~1
goto :eof

:: 检查Node.js版本
:check_node_version
call :print_step "检查Node.js版本..."

node --version >nul 2>&1
if errorlevel 1 (
    call :print_error "Node.js未安装，请先安装Node.js 18.0.0或更高版本"
    pause
    exit /b 1
)

for /f "tokens=1 delims=v" %%i in ('node --version') do set NODE_VERSION=%%i
call :print_message "Node.js版本检查通过: %NODE_VERSION%"
goto :eof

:: 检查npm版本
:check_npm_version
call :print_step "检查npm版本..."

npm --version >nul 2>&1
if errorlevel 1 (
    call :print_error "npm未安装"
    pause
    exit /b 1
)

for /f %%i in ('npm --version') do set NPM_VERSION=%%i
call :print_message "npm版本: %NPM_VERSION%"
goto :eof

:: 安装后端依赖
:install_backend_deps
call :print_step "安装后端依赖..."

cd chat-app-backend

if not exist "package.json" (
    call :print_error "后端package.json文件不存在"
    pause
    exit /b 1
)

call :print_message "清理npm缓存..."
npm cache clean --force

if exist "node_modules" (
    call :print_message "删除旧的node_modules..."
    rmdir /s /q node_modules
)

if exist "package-lock.json" (
    call :print_message "删除旧的package-lock.json..."
    del package-lock.json
)

call :print_message "安装后端依赖包..."
npm install

if errorlevel 1 (
    call :print_error "后端依赖安装失败"
    pause
    exit /b 1
)

call :print_message "后端依赖安装完成"
cd ..
goto :eof

:: 安装前端依赖
:install_frontend_deps
call :print_step "安装前端依赖..."

cd wx-project

if not exist "package.json" (
    call :print_error "前端package.json文件不存在"
    pause
    exit /b 1
)

call :print_message "清理npm缓存..."
npm cache clean --force

if exist "node_modules" (
    call :print_message "删除旧的node_modules..."
    rmdir /s /q node_modules
)

if exist "package-lock.json" (
    call :print_message "删除旧的package-lock.json..."
    del package-lock.json
)

call :print_message "安装前端依赖包..."
npm install

if errorlevel 1 (
    call :print_error "前端依赖安装失败"
    pause
    exit /b 1
)

call :print_message "前端依赖安装完成"
cd ..
goto :eof

:: 构建前端生产版本
:build_frontend
call :print_step "构建前端生产版本..."

cd wx-project

call :print_message "开始构建..."
npm run build

if errorlevel 1 (
    call :print_error "前端构建失败"
    pause
    exit /b 1
)

if exist "dist" (
    call :print_message "前端构建完成，输出目录: wx-project/dist"
) else (
    call :print_error "前端构建失败，dist目录不存在"
    pause
    exit /b 1
)

cd ..
goto :eof

:: 启动开发环境
:start_dev
call :print_step "启动开发环境..."

call :print_message "启动后端服务..."
cd chat-app-backend
start "后端服务" cmd /k "npm start"
cd ..

timeout /t 3 /nobreak >nul

call :print_message "启动前端开发服务器..."
cd wx-project
start "前端服务" cmd /k "npm run dev"
cd ..

call :print_message "开发环境启动完成！"
call :print_message "后端服务: http://127.0.0.1:5000"
call :print_message "前端应用: http://localhost:5173"
call :print_message ""
call :print_message "服务已在新窗口中启动，关闭对应窗口即可停止服务"

pause
goto :eof

:: 启动生产环境
:start_prod
call :print_step "启动生产环境..."

:: 构建前端
call :build_frontend

call :print_message "启动后端生产服务..."
cd chat-app-backend

:: 设置生产环境变量
set NODE_ENV=production
set PORT=5000

npm start

cd ..
goto :eof

:: 显示帮助信息
:show_help
echo ==========================================
echo      PC微信聊天应用部署脚本 v2.0.0
echo ==========================================
echo.
echo 使用方法:
echo   deploy.bat dev     启动开发环境
echo   deploy.bat prod    启动生产环境
echo   deploy.bat install 仅安装依赖
echo   deploy.bat build   仅构建前端
echo   deploy.bat help    显示帮助信息
echo.
echo 开发环境:
echo   - 后端: http://127.0.0.1:5000
echo   - 前端: http://localhost:5173
echo   - 支持热重载和开发工具
echo.
echo 生产环境:
echo   - 构建优化的前端资源
echo   - 启动生产模式后端服务
echo.
echo 测试账号 (密码统一为 123456):
echo   - goddlts (教瘦)
echo   - aaa123 (阿玛尼)
echo   - balala123 (巴拉拉)
echo   - dalao (大佬)
echo   - jiajia1998 (佳佳)
echo   - dabai (大白机器人)
echo.
pause
goto :eof

:: 主函数
:main
echo ==========================================
echo      PC微信聊天应用部署脚本 v2.0.0
echo ==========================================
echo.

if "%~1"=="" (
    call :show_help
    exit /b 1
)

if /i "%~1"=="dev" (
    call :check_node_version
    call :check_npm_version
    call :install_backend_deps
    call :install_frontend_deps
    call :start_dev
) else if /i "%~1"=="prod" (
    call :check_node_version
    call :check_npm_version
    call :install_backend_deps
    call :install_frontend_deps
    call :start_prod
) else if /i "%~1"=="install" (
    call :check_node_version
    call :check_npm_version
    call :install_backend_deps
    call :install_frontend_deps
    call :print_message "所有依赖安装完成！"
    pause
) else if /i "%~1"=="build" (
    call :check_node_version
    call :check_npm_version
    call :build_frontend
    call :print_message "前端构建完成！"
    pause
) else if /i "%~1"=="help" (
    call :show_help
) else (
    call :print_error "未知参数: %~1"
    call :show_help
    exit /b 1
)

goto :eof

:: 执行主函数
call :main %*
